/* Copyright 2014 Nate Eisner, Noah Bahr
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.uwp.kcwm;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

public class ExhibitVideo extends Activity {
	private String path;
	private VideoView video;
	ProgressDialog pDialog;
	private MediaController ctlr;
	String exhibitNum;
	// URL for webserver
	private static final String SITEURL = "http://gracefullyengineered.com/kcwm/";

	/**
	 * When activity is created load and play video
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().hide();
		setContentView(R.layout.video);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			exhibitNum = extras.getString("exhibitNum");
		}
		path = SITEURL + exhibitNum + "/video.mp4";
		video = (VideoView) findViewById(R.id.exhibitVideoView);
		//progress dialog...
		pDialog = new ProgressDialog(ExhibitVideo.this);
		pDialog.setMessage("Loading...");
		pDialog.setIndeterminate(false);
		pDialog.setCancelable(false);
		pDialog.show();
		//try to load url of video
		try {
			ctlr = new MediaController(ExhibitVideo.this);
			ctlr.setAnchorView(video);
			Uri uri = Uri.parse(path);
			video.setMediaController(ctlr);
			video.setVideoURI(uri);
		} catch (Exception e) {
			Log.e("Error", e.getMessage());
			e.printStackTrace();
		}
		video.requestFocus();
		video.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				pDialog.dismiss();
				video.start();
			}
		});
	}
}
